package com.headerfooter.songhang.smartheaderfooterrecyclerview;

import com.headerfooter.songhang.library.SmartRecyclerAdapter;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.utils.PacMap;

import java.util.Optional;

/**
 * 主页面
 */
public class MainAbility extends Ability implements Component.ClickedListener{
    private Text title;
    private Image menu;
    private ListContainer recyclerView;
    private DirectionalLayout headerView;
    private DirectionalLayout footerView;
    private MenuDialog menuDialog;
    private DirectionalLayout toastLayout;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        title = (Text) findComponentById(ResourceTable.Id_title);
        menu = (Image) findComponentById(ResourceTable.Id_menu);

        recyclerView = (ListContainer) findComponentById(ResourceTable.Id_recyclerview);

        menu.setClickedListener(component1 -> {
            int screenWidth = AttrHelper.vp2px(250, getContext());
            int menuHeight = AttrHelper.vp2px(150, getContext());
            menuDialog = new MenuDialog(getContext(), menu, screenWidth, menuHeight, ResourceTable.Layout_menu_tab);
            menuDialog.initListener(MainAbility.this);
            menuDialog.show();
        });
        initHeadAndFooterView();

        showLine();
        title.setText("LinearLayoutManager");
    }

    private void initHeadAndFooterView() {
        // header view
        ComponentContainer.LayoutConfig layoutParams = new ComponentContainer.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_PARENT, 400);
        layoutParams.setMargins(10, 10, 10, 10);

        headerView = new DirectionalLayout(getContext());
        headerView.setLayoutConfig(layoutParams);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(new RgbColor(0, 0, 255));
        headerView.setBackground(background);
        Text head = new Text(getContext());
        head.setLayoutConfig(new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT));
        head.setTextColor(Color.WHITE);
        head.setTextAlignment(TextAlignment.CENTER);
        head.setText("THIS IS HEADER VIEW");
        head.setTextSize(14, Text.TextSizeType.VP);
        headerView.addComponent(head);

        // footer view
        footerView = new DirectionalLayout(getContext());
        footerView.setLayoutConfig(layoutParams);
        background = new ShapeElement();
        background.setRgbColor(new RgbColor(255, 0, 0));
        footerView.setBackground(background);
        Text footer = new Text(getContext());
        footer.setLayoutConfig(new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT));
        footer.setText("THIS IS FOOTER VIEW");
        footer.setTextAlignment(TextAlignment.CENTER);
        footer.setTextColor(Color.WHITE);
        footer.setTextSize(14, Text.TextSizeType.VP);
        footerView.addComponent(footer);
    }

    private void showLine() {
        toastLayout = (DirectionalLayout) LayoutScatter.getInstance(this).parse(
                ResourceTable.Layout_layout_toast, null, false);
        Text text = (Text) toastLayout.findComponentById(ResourceTable.Id_msg_toast);
        text.setText("LinerLayoutManager");
        new ToastDialog(getContext())
                .setContentCustomComponent(toastLayout)
                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                .setTransparent(true)
                .setAlignment(LayoutAlignment.BOTTOM)
                .setOffset(0, 100)
                .show();

        BaseItemProvider adapter = new BaseItemProvider() {
            @Override
            public int getCount() {
                return 50;
            }

            @Override
            public Object getItem(int index) {
                return Optional.ofNullable(null);
            }

            @Override
            public long getItemId(int index) {
                return 0;
            }

            @Override
            public Component getComponent(int index, Component component, ComponentContainer componentContainer) {
                DirectionalLayout cardView = new DirectionalLayout(getContext());

                DirectionalLayout.LayoutConfig layoutParams = new DirectionalLayout.LayoutConfig(
                        DirectionalLayout.LayoutConfig.MATCH_PARENT, 100);
                layoutParams.setMargins(10, 10, 10, 10);
                cardView.setLayoutConfig(layoutParams);

                ShapeElement background = new ShapeElement();
                background.setRgbColor(new RgbColor(255, 255, 255));
                cardView.setBackground(background);

                Text textView = new Text(getContext());
                textView.setLayoutConfig(new DirectionalLayout.LayoutConfig(
                        DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT));
                textView.setTextAlignment(TextAlignment.CENTER);
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(14, Text.TextSizeType.VP);
                textView.setText("position: " + index);

                cardView.addComponent(textView);
                return cardView;
            }
        };

        DirectionalLayoutManager linearLayoutManager = new DirectionalLayoutManager();
        SmartRecyclerAdapter smartRecyclerAdapter = new SmartRecyclerAdapter(adapter);
        smartRecyclerAdapter.setHeaderView(headerView);
        smartRecyclerAdapter.setFooterView(footerView);

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemProvider(smartRecyclerAdapter);
    }

    @Override
    public void onClick(Component component) {
        Intent intent = new Intent();
        Operation operation;
        switch (component.getId()) {
            case ResourceTable.Id_menu1:
                operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.headerfooter.songhang.smartheaderfooterrecyclerview")
                        .withAbilityName("com.headerfooter.songhang.smartheaderfooterrecyclerview.MainAbility")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
                terminateAbility();
                break;
            case ResourceTable.Id_menu2:
                operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.headerfooter.songhang.smartheaderfooterrecyclerview")
                        .withAbilityName("com.headerfooter.songhang.smartheaderfooterrecyclerview.GridAbility")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
                terminateAbility();
                break;
            case ResourceTable.Id_menu3:
                operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.headerfooter.songhang.smartheaderfooterrecyclerview")
                        .withAbilityName("com.headerfooter.songhang.smartheaderfooterrecyclerview.StaggeredAbility")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
                terminateAbility();
                break;
            default:
                break;
        }
        menuDialog.destroy();
    }

    @Override
    protected void onBackPressed() {
        terminateAbility();
    }

    @Override
    public void onSaveAbilityState(PacMap outState) {
        super.onSaveAbilityState(outState);
        if(menuDialog != null && menuDialog.isShowing()){
            outState.putBooleanValue("isShowMenu", true);
        }
    }

    @Override
    public void onRestoreAbilityState(PacMap inState) {
        super.onRestoreAbilityState(inState);
        boolean isShowMenu = inState.getBooleanValue("isShowMenu");
        if(isShowMenu){
            new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                @Override
                public void run() {
                    int screenWidth = AttrHelper.vp2px(250, getContext());
                    int menuHeight = AttrHelper.vp2px(150, getContext());
                    menuDialog = new MenuDialog(getContext(), menu, screenWidth, menuHeight, ResourceTable.Layout_menu_tab);
                    menuDialog.initListener(MainAbility.this);
                    menuDialog.show();
                }
            });
        }
    }
}